package com.gexin.controller.xapp.order;


import com.gexin.common.model.ApiResponse;
import com.gexin.common.utils.IdGenerateUtils;
import com.gexin.core.web.AppController;
import com.gexin.model.order.*;
import com.gexin.service.order.OrderService;
import com.jfinal.ext.route.ControllerBind;

import java.math.BigDecimal;

@ControllerBind(controllerKey = "/xapp/product/order")
public class OrderController extends AppController {

	/**
	 * 申请退款
	 */
	public void reimburse(){
		String order_id =getPara("order_id");
		Order o = Order.dao.findById(order_id);
		if (o == null) {
			fail("订单ID不存在！");
			return;
		}
		if (o.getState() != Order.State_Wait_Send_Express) {
			fail("订单状态不是已发货状态！");
			return;
		}
		if(o.getType()==2){
			OrderPresale is_sf=OrderPresale.dao.findByOrderIdAndType(o.getId(),1);//查订金
			OrderPresale is_wk=OrderPresale.dao.findByOrderIdAndType(o.getId(),2);//查尾款
			if(is_sf.getState()==1){
				fail("抱歉：预售商品不能退款 ");
				return;
			}else if(is_sf.getState()==2 && is_wk.getState()==1){
				fail("抱歉：预售商品不能退款 ");
				return;
			}
		}
		OrderItem item =OrderItem.dao.getOrderItemByOrderId(o.getId());
		item.setRefundState(2);
		OrderRefundApply refundApply = new OrderRefundApply();
		refundApply.setItemId(item.getItemId());
		refundApply.setOrderId(o.getId());
		refundApply.setIsAddFreight(1);
		refundApply.setIsReceiveGoods(2);
		refundApply.setRefundMode(1);
		refundApply.setRefundAmount(o.getTotalAmount());
		refundApply.setRefundPhone(o.getExpressPhone());
		refundApply.save();
		o.setState(6);
		o.update();
		item.update();
		success("申请成功，请等待客服审核");
	}

	/**
	 * 添加商品到购物车
	 */
	public void addCart(){
		Integer productId = getParaToInt("product_id");
		String skuTxt = getPara("sku_txt");
		String skuId = getPara("sku_id");
		ApiResponse apiResponse = OrderService.dao.addProductInCart(productId , skuTxt , skuId , getAuthUser());
		renderJson(apiResponse);
	}


}
